# VCP Study Replication Script

library(tidyverse)
library(stm)
library(wordcloud)
library(igraph)
library(ggplot2)

# Read in and clean data to prepare for structural topic model. Estimate structural topic model.
data <- read_csv("pradhantextall2.csv")
processed <- textProcessor(data$text, metadata = data)
out <- prepDocuments(processed$documents, processed$vocab, processed$meta)
docs <- out$text
vocab <- out$vocab
meta <-out$meta
set.seed(02138)
PradPrevFit <- stm(documents = out$documents, vocab = out$vocab, K = 20, prevalence =~ subject + sday + weekend + morning, max.em.its = 200, data = out$meta, init.type = "Spectral")

# Summarize topics for input into Table 1 (this code provides the information used for the table)

pdf('./tables/AppendixTable1.pdf')
par(mfrow = c(1,2), fin = c(6,7), adj = 0.4)
plot(PradPrevFit, type = 'labels', topics = c(1:10), n = 9, labeltype = 'prob', main = 'Appendix Table 1')
par(mfrow = c(1,2), fin = c(6,7), adj = 0.4)
plot(PradPrevFit, type = 'labels', topics = c(11:20), n = 9, labeltype = 'prob', main = 'Appendix Table 1 continued')
dev.off()


# Figure 1
pdf('./figures/AppendixFigure1.pdf')
set.seed(02138)
cloud(PradPrevFit, topic = 17, scale = c(2,.60))
dev.off()

# Figure 2
pdf('./figures/AppendixFigure2.pdf')
set.seed(02139)
cloud(PradPrevFit, topic = 14, scale = c(2,.60))
dev.off()

# Figure 3 
pdf('./figures/AppendixFigure3.pdf')
set.seed(02139)
cloud(PradPrevFit, topic = 19, scale = c(2,.60))
dev.off()

# Figure 4
pdf('./figures/AppendixFigure4.pdf')
set.seed(02139)
plot(PradPrevFit, main = NULL, xlab = NULL, type = "summary", xlim = c(0, 1))
dev.off()

# Multivariate analysis discussed in section "Modeling Topics and Their Correlates." Full results for each model are reported in the Appendix"

out$meta$morning <- as.factor(out$meta$morning)
set.seed(01238)
prep <- estimateEffect(1:20 ~ subject + sday + weekend + morning, PradPrevFit, meta = out$meta, uncertainty = "Global")
print(summary(prep))
# Summarize models for all topics (reports the results of all models for all 20 topics)
summary(prep)
# Summarize models for individual topics (each reports the results of the model for the specific topic listed in the code)

sink('./tables/AppendixTable2.txt')
summary(prep, topics=1)
sink()

sink('./tables/AppendixTable3.txt')
summary(prep, topics=2)
sink()

sink('./tables/AppendixTable4.txt')
summary(prep, topics=3)
sink()

sink('./tables/AppendixTable5.txt')
summary(prep, topics=4)
sink()

sink('./tables/AppendixTable6.txt')
summary(prep, topics=5)
sink()

sink('./tables/AppendixTable7.txt')
summary(prep, topics=6)
sink()

sink('./tables/AppendixTable8.txt')
summary(prep, topics=7)
sink()

sink('./tables/AppendixTable9.txt')
summary(prep, topics=8)
sink()

sink('./tables/AppendixTable10.txt')
summary(prep, topics=9)
sink()

sink('./tables/AppendixTable11.txt')
summary(prep, topics=10)
sink()

sink('./tables/AppendixTable12.txt')
summary(prep, topics=11)
sink()

sink('./tables/AppendixTable13.txt')
summary(prep, topics=12)
sink()

sink('./tables/AppendixTable14.txt')
summary(prep, topics=13)
sink()

sink('./tables/AppendixTable15.txt')
summary(prep, topics=14)
sink()

sink('./tables/AppendixTable16.txt')
summary(prep, topics=15)
sink()

sink('./tables/AppendixTable17.txt')
summary(prep, topics=16)
sink()

sink('./tables/AppendixTable18.txt')
summary(prep, topics=17)
sink()

sink('./tables/AppendixTable19.txt')
summary(prep, topics=18)
sink()

sink('./tables/AppendixTable20.txt')
summary(prep, topics=19)
sink()

sink('./tables/AppendixTable21.txt')
summary(prep, topics=20)
sink()
